package com.zut.oss.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zut.oss.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.management.ObjectName;
import java.util.List;

@RestController
public class FileController {

    @Resource
    private FileService fileService;


    @RequestMapping("/testBucket")
    public String test() throws Exception {

        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }

    /**
     * 获得文件路径
     * @param bucket
     * @param ObjectName
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUrl")
    public String getUrl(String bucket, String ObjectName) throws Exception {
        return fileService.getUrl(bucket, ObjectName);
    }

    /**
     * 上传文件
     * @param bucket
     * @param ObjectName
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile multipartFile,String bucket, String ObjectName) throws Exception {
        ObjectName=ObjectName+"/"+multipartFile.getOriginalFilename();
        return fileService.upLoad(multipartFile,bucket, ObjectName);
    }
}
